#include "heroplane.h"
#include "config.h"

HeroPlane::HeroPlane() {
    //初始化加载飞机图片资源
    m_Plane.load(HERO_PATH);

    //初始化飞机坐标
    m_X=(GAME_WIDTH-m_Plane.width())*0.5;
    m_Y=GAME_HEIGHT-m_Plane.height()-100;

    //初始化矩形边框
    m_Rect.setWidth(m_Plane.width());
    m_Rect.setHeight(m_Plane.height());
    m_Rect.moveTo(m_X,m_Y);//将矩形框移动到飞机的位置

    //初始化间隔时间
    m_recorder=0;
}

void HeroPlane::shoot()
{
    //累加时间间隔记录变量
    m_recorder++;

    //如果记录未达到时间发射间隔
    if(m_recorder<BULLET_INTERVAL){
        return;
    }

    //达到发射间隔时间,重置
    m_recorder=0;

    //遍历数组找到free的子弹并发射
    for(int i=0;i<BULLET_NUM;i++){
        if(m_bullets[i].m_Free){
            m_bullets[i].m_Free=false;//发射就不再free了
            //设置子弹坐标(飞机中间部分）
            m_bullets[i].m_X=m_X+m_Rect.width()*0.5-10;
            m_bullets[i].m_Y=m_Y;
            break;//只发射一个
        }
    }

}

void HeroPlane::setPosition(int x, int y)
{
    m_X=x;
    m_Y=y;
    m_Rect.moveTo(m_X,m_Y);

}
